﻿using MiniFox.Components;
using MiniFox.Data;
using MiniFox.Platform.Scripting;
using MiniFox.Platform.State;
using MiniFox.Scripting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Platform
{
    /// <summary>
    /// 
    /// </summary>
    [AutoRegister, SpareComponent(typeof(DataSourceFactory))]
    public class DataSourceFactory : MiniFox.Data.DataSourceFactory
    {
        IStateManagement _stateManagement;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stateManagement"></param>
        public DataSourceFactory(IStateManagement stateManagement)
        {
            _stateManagement = stateManagement;
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual IStateManagement CurrentState
        {
            get { return _stateManagement; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        protected override IDataCommand CreateDataCommand(string name, string cmdText, CommandType cmdType, string connectionName)
        {
            cmdText = cmdText.Resolve(this.CurrentState);
            return base.CreateDataCommand(name, cmdText, cmdType, connectionName);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DataSourceFactory<T> : DataSourceFactory where T : IStateManagement
    {
        T _stateManagement;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stateManagement"></param>
        public DataSourceFactory(T stateManagement) : base(stateManagement)
        {
            _stateManagement = stateManagement;
        }

        /// <summary>
        /// 
        /// </summary>
        protected new virtual T CurrentState
        {
            get { return _stateManagement; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        protected override IDataCommand CreateDataCommand(string name, string cmdText, CommandType cmdType, string connectionName)
        {
            cmdText = cmdText.Resolve(CurrentState);
            return base.CreateDataCommand(name, cmdText, cmdType, connectionName);
        }

    }
}
